package ch09_;

public class Ch9_9 {
	public static final int NUM_QUEENS = 15;
	class Wrapper {
		int count = 0;
		public void increment() {
			count++;
		}
		
		public int getCount() {
			return count;
		}
	}
	public void queen() {
		int[] seq = new int[NUM_QUEENS];
		for (int i = 0; i < seq.length; i++) {
			seq[i] = -1;
		}
		Wrapper wrapper = new Wrapper();
		queen(seq, 0, wrapper);
	}
	
	private void queen(int[] seq, int row, Wrapper wrapper) {
		if (row == NUM_QUEENS) {
			wrapper.increment();
			System.out.println("sol #" + wrapper.getCount());
			for (int i = 0; i < NUM_QUEENS; i++) {
				for (int j = 0; j < NUM_QUEENS; j++) {
					if (seq[i] == j) {
						System.out.print(" Q");
					} else {
						System.out.print(" *");
					}
				}
				System.out.println();
			}
			System.out.println("\n\n");
			return;
		}
		for (int col = 0; col < NUM_QUEENS; col++) {
			boolean ok = true;
			for (int r = 0; r < row; r++) {
				if (seq[r] == col) {
					ok = false;
					break;
				}
				if (Math.abs(row - r) == Math.abs(col - seq[r])) {
					ok = false;
					break;
				}
			}
			if (ok) {
				seq[row] = col;
				queen(seq, row + 1, wrapper);
			}
		}
	}
	
	public static void main(String[] args) {
		Ch9_9 test = new Ch9_9();
		test.queen();
	}
}
